package com.sibco.client.right;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.sibco.client.common.Tools;


/**
 * FunctionTemplateRightDatasetListener
 */
public class FunctionTemplateRightDatasetListener extends AbstractDatasetListener {
	public boolean beforeLoadData(Dataset dataset)
    throws Exception {
 	String superFunctionId=Tools.toStr(dataset.parameters().getString("super_function_id"));
 	String functionTemplateId=Tools.toStr(dataset.parameters().getString("functionTemplateId"));
 	String sql="";
 	if (!superFunctionId.equals("") &&  !superFunctionId.equals("null"))  {
 		sql="select * from (select sf.*,1 isChecked from clt_rt_function sf,clt_rt_function_template_right tr "+
			"where tr.function_template_id="+functionTemplateId+" and sf.function_id=tr.function_id "+  
			"and sf.super_function_id="+superFunctionId+ 
			" union "+ 
			"select sf.*,0 isChecked from clt_rt_function sf where sf.super_function_id="+superFunctionId+ 
			" and sf.function_id not in "+ 
			"(select function_id from clt_rt_function_template_right where "+ 
			"function_template_id="+functionTemplateId+" )) order by show_order ";
 	}
 	else  {
 		sql="select * from (select sf.*,1 isChecked from clt_rt_function sf,clt_rt_function_template_right tr "+
			"where tr.function_template_id="+functionTemplateId+" and sf.function_id=tr.function_id "+  
			"and sf.super_function_id is null "+ 
			" union "+ 
			"select sf.*,0 isChecked from clt_rt_function sf where sf.super_function_id is null "+ 
			" and sf.function_id not in "+ 
			"(select function_id from clt_rt_function_template_right where "+ 
			"function_template_id="+functionTemplateId+" )) order by show_order ";
 	}
	SqlDataset sqlDataset=(SqlDataset)dataset;
	sqlDataset.setSql(sql);
   return true;
 }


  public void afterLoadData(Dataset dataset)
     throws Exception {

  }

}
